<template>
  <div class="container">
    <div class="app-container">
      <el-tree :data="depts" default-expand-all :props="defaultProps" :expand-on-click-node="false">
        <template v-slot="{data}">
          <el-row style="width:100%;height:40px" type="flex" justify="space-between" align="middle">
            <!-- 左侧树 -->
            <el-col>{{ data.name }}</el-col>

            <!-- 右侧结构 -->
            <el-col :span="4">
              <span class="tree-manager">{{ data.managerName }}</span>
              <!-- $event 实参 表示类型 -->
              <el-dropdown @command="handleCommand($event,data.id)">
                <!-- 显示区域内容 -->
                <span class="el-dropdown-link">
                  操作<i class="el-icon-arrow-down el-icon--right" />
                </span>
                <!-- 下拉菜单选项 -->
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item command="add">添加子部门</el-dropdown-item>
                  <el-dropdown-item command="edit">编辑部门</el-dropdown-item>
                  <el-dropdown-item command="del">删除</el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown>
            </el-col>

          </el-row>
        </template>
      </el-tree>
    </div>
    <ShowDia ref="sonShowDia" :amend="amend" :parent-id="parentId" :depts="depts" :show-dialog.sync="showDialog" @amendOff="amend = false" @refresh="refreshList" />
  </div>
</template>
<script>
import { getAllDepartment } from '@/api/department'
import ShowDia from './components/showDia'
import { deleteByIdDepart } from '@/api/department'
export default {
    name: 'Department',
    components: {
        ShowDia
    },
    data() {
        return {
            depts: [
                // {
                //     label: '一级',
                //     children: [{
                //         label: '二级',
                //         children: [{
                //             label: '三级'
                //         }]
                //     }]
                // }
            ],
            defaultProps: {
                children: 'children',
                label: 'label'
            },
            arrrr:
            [
                { id: 1, name: '总裁办', pid: 0 },
                { id: 11, name: '秘书处', pid: 1 },
                { id: 2, name: 'IT部', pid: 0 }
            ],
            showDialog: false,
            // 父级id
            parentId: '',
            // 弹出层修改开关
            amend: false
        }
    },
    async created() {
        await this.refreshList()
    },
    methods: {
        async refreshList() {
            let res = await getAllDepartment()
            this.depts = this.dismantle(res, 0)
            this.$message.success('刷新成功')
        },
        async handleCommand(val, ids) {
            if (val === 'add') {
                this.showDialog = true
                this.parentId = ids
                this.amend = false
            } else if (val === 'edit') {
                // 进入编辑，打开弹框，传入当前 id
                this.parentId = ids
                // 同时要给表单调取初始的值
                // 父组件调用子组件内部的函数
                this.$nextTick(async() => {
                    await this.$refs.sonShowDia.getByIdDepart(ids)
                    // 先有数据，再打开弹窗
                    this.showDialog = true
                    // 打开编辑开关
                    this.amend = true
                })
            } else if (val === 'del') {
                this.$alert('真的要删除嘛？', {
                    confirmButtonText: '确定',
                    callback: async() => {
                        // 删除
                        await deleteByIdDepart(ids)
                        // 刷新页面
                        this.refreshList()
                        this.$message({
                            type: 'success',
                            message: `删除成功`
                        })
                    }
                })
            }
        },
        dismantle(arr, id) { // [], 0
            let newArr = []
            // 第一次导入的id就是起始数据
            // 有嵌套关系的，要被添加chirdren属性
            arr.forEach(ele => { // { "传智教育" }
                // 第一条数据参与运算
                if (ele.pid === id) { // 遍历整个数组，符合条件就被加入
                    // console.log(id) // 0 1 0 为什么最后是 0 ，因为是同级别
                    // 0 === 0?
                    // pid = 1,id = 1
                    // 卡住
                    ele['children'] = this.dismantle(arr, ele.id) // 0,1,11
                    newArr.push(ele)
                }
            })
            return newArr // 只能有唯一返回值，是一个完全体的数组，只包含一个对象
        }

        // var arr = [
        //     { id: 1, name: '总裁办', pid: 0 children},
        //     { id: 11, name: '秘书处', pid: 1 ,children}, []
        //     { id: 2, name: 'IT部', pid: 0 }
        // ]
    }
}
</script>

<style scoped>
.app-container {
  padding: 30px 140px;
  font-size: 14px;
}
.tree-manager {
  width: 50px;
  display: inline-block;
  margin: 10px;
}
.el-tree-node__content {
    height: 40px;
}
</style>

